import Vue from 'vue'
import store from '../store'
import {AjaxPlugin} from 'vux'

// set the vux's AjaxPlugin baseUrl to yours
AjaxPlugin.$http.defaults.baseURL = '/api'

// 添加一个请求拦截器
AjaxPlugin.$http.interceptors.request.use(function (config) {
  // 在请求发送之前做一些事
  if (store.getters.token) {
    // 让每个请求携带token--['X-Token']为自定义key 请根据实际情况自行修改
    config.headers['x-lt-token'] = store.getters.token
  }
  // 在请求发送之前做一些事
  store.commit('SWITCH_IS_LOADING', true)
  return config
})

// 添加一个全局AjaxPlugin返回拦截器
AjaxPlugin.$http.interceptors.response.use(function (res) {
  // 对返回的数据进行一些处理
  store.commit('SWITCH_IS_LOADING', false)
  console.log(res.data)
  if (res.data.result === 0) {
    return res.data.info
  } else {
    Vue.$vux.alert.show({
      title: '提示',
      content: res.data.msg
    })
    return Promise.reject(msg)
  }
  
}, function (error) {
  store.commit('SWITCH_IS_LOADING', false)
  console.error(error.response.data.msg)
  let msg = error.response.data.msg
  // 需要处理的 code
  if (error && error.response) {
    switch (error.response.status) {
      case 404:
        msg = `请求地址出错: ${error.response.config.url}`
        break
    }
  }
  Vue.$vux.alert.show({
    title: '提示',
    content: msg
  })
  return Promise.reject(error)
})

// ===============================================================

/* 将所有接口统一起来便于维护
 * 如果项目很大可以将 url 独立成文件，接口分成不同的模块
 */
export const getAllShow = () => {
  return AjaxPlugin.$http.post('/appNews/getAllShow/')
}
export const getNews = id => {
  return AjaxPlugin.$http.post('/appNews/getNewsById/', {"id":id})
}
export const getInfo = type => {
  return AjaxPlugin.$http.post('/appInfo/getInfoByType/', {"type":type})
}
export const getParameter = id => {
  return AjaxPlugin.$http.post('/common/getDtParameterById/', {"id":id})
}
export const getEnum = enumTypeId => {
  return AjaxPlugin.$http.post('/common/getDtEnum/', {"enumTypeId":enumTypeId})
}
export const getEnum2 = enumTypeId => {
  return AjaxPlugin.$http.post('/common/getDtEnum2/', {"enumTypeId":enumTypeId})
}
export const getLevel = () => {
  return AjaxPlugin.$http.post('/common/getDtLevel/')
}
export const getSubject = () => {
  return AjaxPlugin.$http.post('/common/getDtSubject/')
}
export const getPartner = orderKey => {
  return AjaxPlugin.$http.post('/common/getDtPartner', {"orderKey":orderKey})
} 
export const getBatch = () => {
  return AjaxPlugin.$http.post('/common/getDtBatch')
} 
export const getOrderSetting = data => {
  return AjaxPlugin.$http.post('/common/getDtOrderSetting', data)
} 
export const checkCount = openid => {
  return AjaxPlugin.$http.post('/common/checkCount', {"openid":openid})
}
export const addOrder = data => {
  return AjaxPlugin.$http.post('/order/addOrder', data)
}
export const getDtBatchList = () => {
  return AjaxPlugin.$http.post('/common/getDtBatchList')
}
export const getExamResult = data => {
  return AjaxPlugin.$http.post('/common/getExamResult', data)
} 
export const getDtOrdersToPay = data => {
  return AjaxPlugin.$http.post('/order/getDtOrdersToPay', data)
}
export const getDtOrders = data => {
  return AjaxPlugin.$http.post('/order/getDtOrders', data)
}
export const getDtPicture = data => {
  return AjaxPlugin.$http.post('/common/getDtPicture', data)
}
export const beforeToPay = data => {
  return AjaxPlugin.$http.post('/order/updateDtOrdersToPay', data)
}
export const getOrdersList = data => {
  return AjaxPlugin.$http.post('/order/getDtOrdersList', data)
}
export const getExamNoRead = data => {
  return AjaxPlugin.$http.post('/common/getExamNoRead', data)
}
// 默认全部倒出
export default {
  getAllShow,
  getNews,
  getInfo,
  getParameter,
  getEnum,
  getEnum2,
  getLevel,
  getSubject,
  getPartner,
  getBatch,
  getOrderSetting,
  checkCount,
  getDtBatchList,
  addOrder,
  getExamResult,
  getDtOrdersToPay,
  getDtOrders,
  getDtPicture,
  beforeToPay,
  getOrdersList,
  getExamNoRead,
}
